<template>
	<view>
		<Line />
		<view class="login-tel">
			<view class="tel-main">
				<view class="login-form">
					<view class="login-user form-input">
						<text class="input-text">手机号</text>
						<input type="number" focus="true" v-model="userTel" placeholder="请输入11位手机号">
					</view>
				</view>
				<view class="tel" @tap="goNextCode">下一步</view>
			</view>
		</view>
	</view>
</template>

<script>
	import Line from "@/components/Line/Line.vue"
	import $http from "@/common/api/request.js"
	export default {
		data() {
			return {
				userTel:"",
				//验证的规格
				rules:{
					userTel:{
						rule:/^1[3456789]\d{9}$/,
						msg:"请输入11位手机号"
					}
				}
			};
		},
		methods:{
			goNextCode(){
				if(!this.validate("userTel")) return
				
				$http.request({
					url: "/user/register",
					method:"POST",
					data:{
						phone:this.userTel
					}
				}).then(res => {
					let data = res.data
					if(data.code){
						//下一步
						uni.navigateTo({
							url:"/pages/login-code/login-code?phone="+ this.userTel
						})
					}else{
						//手机号已经存在
						uni.showToast({
							title:data.msg,
							icon:"none"
						})
					}
				}).catch(err => {
					console.log(err);
					uni.showToast({
						title: "请求失败",
						icon: "none"
					})
				})
			},
			//判断验证是否符合规则
			validate(key){
				let bool = true
				if(!this.rules[key].rule.test(this[key])){
					uni.showToast({
						title:this.rules[key].msg,
						icon:"none"
					})
					bool = false
				}
				return bool
			}
		}
	}
</script>

<style lang="scss" scoped>
	.login-tel {
		width: 100vw;
		height: calc(100vh - 44px);
		/* #ifdef APP-PLUS */
		height: 100vh;
		/* #endif */

		.tel-main {
			padding: 0 20rpx;

			.tel {
				width: 100%;
				height: 80rpx;
				line-height: 80rpx;
				text-align: center;
				color: #fff;
				background-color: #49bdfb;
				border-radius: 40rpx;
			}
			//表单
			.login-form {
				padding: 30rpx 0;

				.form-input {
					display: flex;
					align-items: baseline;
					border-bottom: 2rpx solid #f7f7f7;
					font-size: 32rpx;
					padding: 20rpx 0;

					.input-text {
						padding-right: 30rpx;
					}

					input {
						font-size: 28rpx;
					}
				}
			}
		}
	}
</style>
